﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Ornament.MemberShip;
using Ornament.MemberShip.Permissions;
using Ornament.MVCWebFrame.Models;
using Ornament.Web;
using Ornament.Web.MemberShips;
using Ornament.Web.Mvc;

namespace Ornament.MVCWebFrame.Areas.MemberShips.Controllers
{
    /// <summary>
    /// </summary>   
    [NHibernateFilter]
    public class RoleController : Controller
    {
        // GET: /Role/

        /// <summary>
        /// </summary>
        /// <param name="pageIndex">
        /// The page index.
        /// </param>
        /// <returns>
        /// </returns>        
        [ResourceAuthorize(RoleOperator.Read, "Role")]
        public ActionResult Index()
        {
            IRoleDao roleDao = OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().CreateRoleDao();
            ViewData["permissions"] = from p in OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().Permissions select p;

            return View(roleDao.GetAll());
        }

        /// <summary>
        /// </summary>
        /// <param name="role">
        /// The role.
        /// </param>
        /// <returns>
        /// </returns>
        [JsonContainerFilter]
        public ActionResult Save(JsonContainer role)
        {
            Role roleEntity =
                OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().CreateRoleDao().GetByName(role.ToString("Name")) ??
                new Role(role.ToString("Name"));

            roleEntity.Remark = role.ToString("Comment");
            roleEntity.Permissions.Clear();

            IPermissionDao permissionDao = OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().CreatePermissionDao();

            foreach (string id in role.ToArray<string>("Permissions"))
            {
                roleEntity.Permissions.Add(permissionDao.Get(new Guid(id)));
            }
            return Json(new ReturnResult("保存成功", true), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult Delete(string name)
        {
            //string[] roleNames = name.Split(',');

            //IRoleDao dao = OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().CreateRoleDao();
            //string[] canBeDeletedRoles;

            //IList<Role> cannotDeleteRole = dao.GetInUseRoles(roleNames, out canBeDeletedRoles);


            //foreach (Role deleteRole in dao.GetRoles(canBeDeletedRoles))
            //{
            //    if (deleteRole.Name == "admin")
            //        continue;
            //    dao.Delete(deleteRole);
            //}

            //var sb = new StringBuilder();

            //if (canBeDeletedRoles.Length != 0)
            //{
            //    sb.Append("以下Roles已经被删除" + String.Join(",", canBeDeletedRoles));
            //}

            //if (cannotDeleteRole.Count != 0)
            //{
            //    IEnumerable<string> roles = from role in cannotDeleteRole select role.Name;
            //    sb.Append(String.Join(",", roles.ToArray()));
            //}
            //if (cannotDeleteRole.Count == 0 && canBeDeletedRoles.Length == 0)
            //{
            //    sb.Append("没有可以删除的Role");
            //}
            return Json("sucess", JsonRequestBehavior.AllowGet);
        }

        public ActionResult ListRolesByName(string name, int pageSize, int pageIndex)
        {
            IRoleDao roleDao = OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().CreateRoleDao();
            IOrderedQueryable<Role> resultEntities = from role in
                                                         OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().Roles
                                                         .Skip(pageIndex * pageSize)
                                                         .Take(pageSize)
                                                     where role.Name.IndexOf(name) != -1
                                                     orderby role.Name
                                                     select role;
            return Json(resultEntities);
        }
    }
}